#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/openshot/env_read.sh"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/openshot"

check_app_file_path() {
    local file_name="$1"
    local found=false
    for version in "${APP_VERSION_HISTORY[@]}"; do
        APP_PATH="$LMD_APPS_DIR/openshot/v${version}/${file_name}"
        if [[ -f "$APP_PATH" ]]; then
            echo "Found file: $APP_PATH"
            found=true
            break
        else
            echo "File not found: $APP_PATH"
        fi
    done
    if [[ "$found" == false ]]; then
        echo "No files found with the given versions."
    fi
}

convert_path() {
    local win_path="$1"

    # 1. Replace backslashes with forward slashes.
    local linux_path="${win_path//\\//}"

    # 2. Convert the drive letters in the Windows system to lowercase. C: -> /c
    # if [[ $linux_path =~ ^[A-Za-z]: ]]; then
    #     local drive_letter=$(echo "$linux_path" | cut -c1 | tr '[:upper:]' '[:lower:]')
    #     local rest_path=$(echo "$linux_path" | cut -c3-)
    #     linux_path="/$drive_letter/$rest_path"
    # fi

    # 3. clear duplicated "/".
    linux_path=$(echo "$linux_path" | sed 's|//|/|g')
    echo "$linux_path"
}

function start_app_win {
  file_name="openshot-qt.exe"
  APP_PATH="$CURRENT_APP_PATH/install_dir/${file_name}"
  APP_PATH=$(convert_path "$APP_PATH")
  echo "start $APP_PATH"
  "$APP_PATH"
}

source $LMD_BASE_INSTALL_SCRIPT_DIR/apps/openshot/env_read.sh

if [ -f /etc/os-release ]; then
    echo "Linux. Not yet support."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    # echo "macOS"
    INSTALL_APP_PATH="${_MAC_INSTALL_PATH}/${_MAC_INSTALL_TARGET_FILE_NAME}"
    open "$INSTALL_APP_PATH"
elif current_os_is_windows; then
    # echo "Windows"
    start_app_win
else
    echo "Unknown OS"
fi

